package ast

import (
	"fmt"
	"rule/pkg/govaluate"
	"time"
)

func Hello() {
	var a int = 0
	for i := 0; i < 100; i++ {
		a = a + 1
	}
}

func GetTimeUnix() {
	// 毫秒
	tnow := time.Now()

	// 秒时间戳一秒等于1 ; 毫秒时间戳一秒等于1000

	ms := tnow.UnixMilli()
	// time.UnixMilli(int64(ms))
	t1 := time.Date(2024, 7, 26, 14, 49, 59, 00, time.Local) // UTC
	t2 := time.Date(2024, 7, 26, 14, 50, 00, 00, time.Local)

	ms1 := t1.UnixMilli()
	ms2 := t2.UnixMilli()
	// str := t.Format("2006-01-02 15:04:05")
	fmt.Println(ms)

	fmt.Println(ms1, ms2)
	fmt.Println(ms2 - ms1)
}

func feiqi_code() {
	// _2000844537@60,_2000844539@60,_2000844541@60,_2000844543@60
	// (${_2000844537} != 1 && ${_2000844539} != 1 && ${_2000844541} != 1 && ${_2000844543} != 1)
	// ${_2000844537} != 1,${_2000844539} != 1,${_2000844541} != 1,${_2000844543} != 1

	// 旧代码, 后续都要删除
	// "sum(1, sum(2, 3), 2 + 2, true ? 4 : 5)"
	var expressionStr string
	// expressionStr = "(a2000844537 > 5 && a2000844539 < 10)"
	expressionStr = "sum(1, sum(2, 3), 2 + 2, true ? 4 : 5)"

	functions1 := map[string]govaluate.ExpressionFunction{
		"sum": func(arguments ...interface{}) (interface{}, error) {
			var sum float64 = 0
			for _, v := range arguments {
				sum += v.(float64)
			}
			return sum, nil
		},
	}

	//parameters := make(map[string]interface{}, 2)
	//parameters["a2000844537"] = 6
	//parameters["a2000844539"] = 9

	// functions := make(map[string]ExpressionFunction)
	//	return NewEvaluableExpressionWithFunctions(expression, functions)

	expression, err := govaluate.NewEvaluableExpressionWithFunctions(expressionStr, functions1)
	if err != nil {
		fmt.Println("Error creating expression:", err)
		return
	}

	result, err := expression.Evaluate(nil)
	if err != nil {
		fmt.Println("Error evaluating expression:", err)
		return
	}

	fmt.Println("result raw", result)
	boolResult, ok := result.(bool)
	if !ok {
		fmt.Println("Result is not a boolean")
		return
	}

	fmt.Println("The result of the expression is:", boolResult)

	// 	_2000844537@60,_2000844539@60,_2000844541@60,_2000844543@60
	// (${_2000844537} != 1 && ${_2000844539} != 1 && ${_2000844541} != 1 && ${_2000844543} != 1)
	// ${_2000844537} != 1,${_2000844539} != 1,${_2000844541} != 1,${_2000844543} != 1

	// strconv.ParseBool
	// github.com/Knetic/govaluate

	//fmt.Println("Hello

	// expression, err := govaluate.NewEvaluableExpression("foo > 0");
	// parameters := make(map[string]interface{}, 8)
	// parameters["foo"] = -1;

	// result, err := expression.Evaluate(parameters);

}
